/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.tms.module.station.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;

import javax.validation.Valid;

import org.apache.logging.log4j.util.Strings;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.tms.global.exception.CommonException;
import org.springblade.tms.module.station.entity.TMStation;
import org.springblade.tms.module.station.service.IStationService;
import org.springblade.tms.module.station.vo.StationVo;
import org.springblade.tms.module.station.vo.TMStationVO;
import org.springblade.tms.module.station.wrapper.StationWrapper;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.boot.ctrl.BladeController;

import java.util.List;

/**
 * 港口/车站 控制器
 *
 * @author Blade
 * @since 2022-07-18
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-station/station")
@Api(value = "港口/车站", tags = "港口/车站接口-yr")
public class StationController extends BladeController {

	private IStationService stationService;

	/**
	 * 分页 港口/车站
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入station")
	public R<IPage<TMStationVO>> list(TMStation tMStation, Query query) {
		IPage<TMStation> pages = stationService.page(Condition.getPage(query), Condition.getQueryWrapper(tMStation));
		return R.data(StationWrapper.build().pageVO(pages));
	}

	/**
	 * 新增或修改 港口/车站
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入station")
	public R submit(@Valid @RequestBody TMStation tMStation) {
		TMStation one = stationService.lambdaQuery()
			.eq(TMStation::getCode, tMStation.getCode())
			.eq(TMStation::getType, tMStation.getType())
			.one();
		if (one!=null){
			CommonException.throwCommonException("编码重复，请重新填写！");
		}
		return R.status(stationService.saveOrUpdate(tMStation));
	}

	/**
	 * 删除 港口/车站
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(stationService.deleteLogic(Func.toLongList(ids)));
	}

	/**
	 * 分页 港口/车站
	 */
	@GetMapping("/dropDownBox")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "下拉框", notes = "传入station")
	public R<List<StationVo>> list(TMStation tMStation) {
		List<TMStation> list = stationService.lambdaQuery()
			.eq(TMStation::getType, tMStation.getType())
			.like(Strings.isNotBlank(tMStation.getDescribe()), TMStation::getDescribe, tMStation.getDescribe())
			.list();
		return R.data(BeanUtil.copy(list, StationVo.class));
	}

}
